package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TActivity;
import com.quwan.manager.service.SystemActivityService;
import com.quwan.mapper.TActivityMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author laozhang
 * @date 2021/11/23 16:26
 */
@Service
@RequiredArgsConstructor
public class SystemActivityServiceImpl implements SystemActivityService {

    private final TActivityMapper activityMapper;
    private final OssConfig ossConfig;

    @Override
    public PageResult<TActivity> getActivityListPage(int page, int size,Integer id, String name) {
        LambdaQueryWrapperX<TActivity> wrapper = new LambdaQueryWrapperX<TActivity>()
                .orderByDesc(TActivity::getAddTime);
        if (id !=null &&id>0) {
            wrapper.eq(TActivity::getId,id);
        }
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(TActivity::getTitle,name);
        }

        Page<TActivity> activityPage = activityMapper.selectPage(new Page<>(page, size), wrapper);
        List<TActivity> resList = activityPage.getRecords();
        /* 开始遍历 */
        for (TActivity ban : resList) {
            if(ban.getImgUrl()!= null && ban.getImgUrl().length()>0){
                ban.setImgUrl(ossConfig.getHostname()+"/"+ban.getImgUrl());
            }
        }
        PageResult<TActivity> result = new PageResult<TActivity>()
                .setCurrent(activityPage.getCurrent())
                .setPages(activityPage.getPages())
                .setSize(activityPage.getSize())
                .setTotal(activityPage.getTotal())
                .setRecords(resList);
        return result;
    }

    @Override
    public List<TActivity> getActivityList(Integer id, String name) {
        LambdaQueryWrapperX<TActivity> wrapper = new LambdaQueryWrapperX<TActivity>()
                .orderByDesc(TActivity::getAddTime);
        if (id !=null &&id>0) {
            wrapper.eq(TActivity::getId,id);
        }
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(TActivity::getTitle,name);
        }
        List<TActivity> resList = activityMapper.selectList(wrapper);
        /* 开始遍历 */
        for (TActivity ban : resList) {
            if(ban.getImgUrl()!= null && ban.getImgUrl().length()>0){
                ban.setImgUrl(ossConfig.getHostname()+"/"+ban.getImgUrl());
            }
        }
        return resList;
    }

    @Override
    public boolean addActivity(TActivity activity) {
        return activityMapper.insert(activity) == 1;
    }

    @Override
    public boolean updateActivity(TActivity activity) {
        return activityMapper.updateById(activity) == 1;
    }

    @Override
    public TActivity detailActivity(Integer id) {
        TActivity res = activityMapper.selectById(id);
        if(res.getImgUrl().length()>0){
            res.setImgUrl(ossConfig.getHostname()+"/"+res.getImgUrl());
        }
        return res;
    }

    @Override
    public boolean delActivity(Integer id) {
        return activityMapper.deleteById(id) == 1;
    }
}
